#define _CRT_SECURE_NO_WARNINGS 1
#include "Queue.h"


void QInit(Queue* pf)
{
	assert(pf);
	pf->head = pf->tail = NULL;
	pf->size = 0;
}
void QDestotry(Queue* pf)
{
	assert(pf);
	QueueNode* cur = pf->head;
	while (cur)
	{
		QueueNode* next = cur->next;
		free(cur);
		cur = next;
	}
	pf->head = pf->tail = NULL;
	pf->size = 0;
}

void QueuePush(Queue* pf, QTypeData x)
{
	assert(pf);
	QueueNode* newnode = (QueueNode*)malloc(sizeof(QueueNode));
	if (newnode == NULL)
	{
		perror("malloc fail");
		return;
	}
	newnode->data = x;
	newnode->next = NULL;

	if (pf->head == NULL)
	{
		assert(pf->tail == NULL);
		pf->head =pf->tail= newnode;
	}
	else
	{
		pf->tail->next = newnode;
		pf->tail = newnode;
	}
	pf->size++;
}


void QueuePop(Queue* pf)
{
	assert(pf);
	assert(pf->head !=NULL);
	if (pf->head->next == NULL)
	{
		free(pf->head);
		pf->head = NULL;
	}
	else
	{
		QueueNode* next = pf->head->next;
		free(pf->head);
		pf->head = next;

	}
	pf->size--;
}

int Qsize(Queue* pf)
{
	assert(pf);
	return pf->size;
}
bool QEmpty(Queue* pf)
{
	assert(pf);
	return pf->size == 0;
}
QTypeData QueueFront(Queue* pf)
{
	assert(pf);
	assert(!QEmpty(pf));
	return pf->head->data;
}
QTypeData QueueBack(Queue* pf)
{
	assert(pf);
	assert(!QEmpty(pf));
	return pf->tail->data;
}